package com.bhs.sansonglogistics.ui.enterprise;

import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.CollaborationInform;
import com.bhs.sansonglogistics.dialog.BottomSelectiveLineDialog;
import com.bhs.sansonglogistics.dialog.MyClickListener2;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.CollaborationAdapter;
import com.bhs.sansonglogistics.utils.LogUtils;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 企业协作
 */
public class CollaborationFragment extends BaseFragment implements NetCallBack {
    private int type;
    private RecyclerView mRvList;
    private CollaborationAdapter mAdapter;
    private SwipeRefreshLayout mRefreshLayout;
    private int page=1;

    public static CollaborationFragment newInstance(int type) {
        CollaborationFragment fragment = new CollaborationFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_collaboration;
    }

    @Override
    public void initView(View root) {
        assert getArguments() != null;
        type = getArguments().getInt("type");
        mRvList = root.findViewById(R.id.rv_list);
        mAdapter = new CollaborationAdapter(type, SharedPreferencesUtils.getInt(getContext(), "logistics_id", 0));
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
        //监听 刷新是回调
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();

            }
        });

    }

    @Override
    public void onResumes() {
        super.onResumes();
        page = 1;
        loadData();
    }

    private void loadData() {
        if (type == 1) {
            networkRequest(netApi.get_collaborate_list(page), this);
        } else {
            networkRequest(netApi.get_collaborate_notice_list(page), this);
        }
    }

    @Override
    public void initData() {
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CollaborationInform.DataDTO.ListDTO info = mAdapter.getData().get(position);
                Map<String, String> map = new ArrayMap<>();
                map.put("notice_id", String.valueOf(info.getNotice_id()));
                //同意
                if (view.getId() == R.id.tv_consent) {
                    audit_collaborate(map);
                } else if (view.getId() == R.id.tv_refuse) {
                    if (type == 1) {
                        //解除协作
                        networkRequest(netApi.delete_collaborate(info.getCollaborate_id()), CollaborationFragment.this, 9527);
                    } else {
                        //拒绝
                        map.put("audit_status", "2");
                        networkRequest(netApi.audit_collaborate(map), CollaborationFragment.this, 9528);
                    }
                }
            }
        });
    }

    BottomSelectiveLineDialog bottomSelectiveLineDialog;

    private void audit_collaborate(Map<String, String> map) {
        if (bottomSelectiveLineDialog == null) {
            bottomSelectiveLineDialog = new BottomSelectiveLineDialog(this, 2);
        }
        bottomSelectiveLineDialog.setMyClickListener(new MyClickListener2() {
            @Override
            public void onClick(String id, String info) {
                map.put("audit_status", "1");
                map.put("to_line_id", id);
                map.put("to_is_show_address", info);
                networkRequest(netApi.audit_collaborate(map), CollaborationFragment.this, 9529);
            }
        });
        bottomSelectiveLineDialog.show();

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        loadData();
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 0) {
            CollaborationInform bean = new Gson().fromJson(json, CollaborationInform.class);
            mRefreshLayout.setRefreshing(false);
            if (bean.isStatus()) {
                if (page == 1) {
                    mAdapter.setNewData(bean.getData().getList());
                } else {
                    mAdapter.loadMoreComplete();
                    mAdapter.addData(bean.getData().getList());
                }
                if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                    mAdapter.loadMoreEnd();
                }
            }
        } else {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);

            if (bean.isStatus()) {
                if (requestCode == 9528) {
                    ToastUtil.show("操作成功");
                } else {
                    ToastUtil.show(bean.getMsg());
                }
            } else {
                ToastUtil.show(bean.getMsg());
            }
            page = 1;
            loadData();
        }
    }

    @Override
    public void onFailure(int requestCode) {
        if (requestCode == 0) {
            mRefreshLayout.setRefreshing(false);
            mAdapter.loadMoreComplete();
        }
    }
}